home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 410_01 / wlist / wlist.h < prev   
Encoding:
C/C++ Source or Header  |  1995-12-30  |  8.9 KB  |  242 lines

  1. #ifndef WLIST_INCLUDE
  2. #define WLIST_INCLUDE 1
  3. static char SCCS_WLIST_H[] = "@(#)wlist.h    1.1 2/15/94 10:38:14";
  4. /*+++
  5.  
  6.     wlist.h
  7.  
  8.     PURPOSE : C++ list class definition
  9.  
  10.     DATE    : Sat Jan 29 11:41:20 EST 1994
  11.  
  12.     AUTHOR  : W. Hatch
  13.  
  14.     PROJECT : WEH Software
  15.             
  16.     COMPANY : Coleman Research Corporation
  17.           9891 Broken Land Parkway
  18.           Suite 200
  19.           Columbia, Maryland 21045
  20.           Phone (301)621-8600
  21.           FAX (410)7210
  22. ---*/ 
  23. /*
  24. ------------------------------------------------------------------------
  25.   MODIFICATIONS
  26. DATE-PROGRAMMER    DESCRIPTION
  27. ------------------------------------------------------------------------
  28. 2-15-94 W. Hatch    added wlist functions ExchangePrevious(),
  29.             ExchangeNext(), Maximum(), Minimum(), Sort(),
  30.             Reverse(), Copy(), SetCompareData()
  31. */
  32. #include <string.h>
  33. #undef  TRUE
  34. #define TRUE 1
  35. #undef FALSE
  36. #define FALSE 0
  37. #undef streql
  38. #define streql(a,b) (!strcmp((a),(b)))
  39.  
  40. #define PROBLEM(s) \
  41. printf("\t%s problem at file %s line %d\n",s,__FILE__,__LINE__)
  42.  
  43.  
  44. class node{
  45. private:
  46.     node *previous;
  47.     node *next;
  48.     void *data;
  49. public:
  50.     node();
  51.     ~node();
  52.     node *PreviousNode();
  53.     node *PreviousNode(node *v);
  54.     node *NextNode();
  55.     node *NextNode(node *v);
  56.     void *NodeData();
  57.     void *NodeData(void *v);
  58.  
  59.     void print(FILE *pf);
  60.     void print();
  61. };
  62.  
  63. class wlist {
  64. private:
  65.     char *name;
  66.     int  size;
  67.     node *first;
  68.     node *last;
  69.     node *current;
  70.     void (*PrintData)(FILE *pf, void*);
  71.     int  (*CompareData)(void *a, void *b);
  72. public:
  73.     //----------------------------------------------------------------
  74.     // constructor - set first, last, current to null; name to "noname"
  75.     //    set size to zero
  76.     //----------------------------------------------------------------
  77.     wlist();
  78.     //----------------------------------------------------------------
  79.     // destructor - delete each node; delete name
  80.     //----------------------------------------------------------------
  81.     ~wlist();
  82.     //----------------------------------------------------------------
  83.     // Size - return the number of list nodes
  84.     //----------------------------------------------------------------
  85.     int Size();
  86.     //----------------------------------------------------------------
  87.     // DeleteAll - delete all list nodes, set size to zero - does NOT
  88.     //    delete the node contents and this is a potential source
  89.     //    of memory leakage
  90.     //----------------------------------------------------------------
  91.     void wlist::DeleteAll();
  92.     //----------------------------------------------------------------
  93.     // Name - access and assign list name
  94.     //----------------------------------------------------------------
  95.     char *Name();
  96.     char *Name(char *s);
  97.  
  98.     //----------------------------------------------------------------
  99.     // FirstData - access and assign data in first node; set current to
  100.     //    first node; on assign, if list is empty, then AppendData()
  101.     //----------------------------------------------------------------
  102.     void *FirstData();
  103.     void *FirstData(void *v);
  104.  
  105.     //----------------------------------------------------------------
  106.     // LastData - access and assign data in last node; set current to
  107.     //    last node; on assign, if list is empty, then AppendData() 
  108.     //----------------------------------------------------------------
  109.     void *LastData();
  110.     void *LastData(void *v);
  111.  
  112.     //----------------------------------------------------------------
  113.     // CurrentData - access and assign data in current node;
  114.     //     on assign, if list is empty, then AppendData() 
  115.     //----------------------------------------------------------------
  116.     void *CurrentData();
  117.     void *CurrentData(void *v);
  118.  
  119.     //----------------------------------------------------------------
  120.     // NextData - access and assign data in next node; set current to
  121.     //    next node; on assign, if list is empty, then AppendData() 
  122.     //    if current is last then AppendData()
  123.     //----------------------------------------------------------------
  124.     void *NextData();
  125.     void *NextData(void *v);
  126.  
  127.     //----------------------------------------------------------------
  128.     // PreviousData - access and assign data in previous node; set 
  129.     //    current to previous node; on assign, if list is empty, 
  130.     //    then AppendData(), if current == first then PrependData() 
  131.     //----------------------------------------------------------------
  132.     void *PreviousData();
  133.     void *PreviousData(void *v);
  134.  
  135.     //----------------------------------------------------------------
  136.     // AppendData - creates a new node containing the given data and
  137.     //    appends this node to end of list; new node becomes the
  138.     //    current node
  139.     //----------------------------------------------------------------
  140.     void *AppendData(void *v);
  141.  
  142.     //----------------------------------------------------------------
  143.     // PrependData - creates a new node containing the given data and
  144.     //    adds this node to beginning of list; new node becomes
  145.     //    the current node
  146.     //----------------------------------------------------------------
  147.     void *PrependData(void *v);
  148.  
  149.     //----------------------------------------------------------------
  150.     // PreInsert - Insert node containing given data before the
  151.     //    current node. The new node becomes the current node
  152.     //----------------------------------------------------------------
  153.     void *PreInsert(void *v);
  154.  
  155.     //----------------------------------------------------------------
  156.     // PostInsert - Insert node containing given data after the
  157.     //    current node. The new node becomes the current node
  158.     //----------------------------------------------------------------
  159.     void *PostInsert(void *v);
  160.  
  161.     //----------------------------------------------------------------
  162.     // DeleteCurrent - delete the current node. If only one node
  163.     //     remains, then this becomes the new current node. If deleted
  164.     //    node is the last node, then the new current node is the
  165.     //    next to last node. If the deleted node is the first node,
  166.     //    then the new current node is the second node.  If more
  167.     //    than one node remains and an interior node is deleted,
  168.     //    then the new current node is the "next node" to the
  169.     //    deleted node.
  170.     //----------------------------------------------------------------
  171.     void *DeleteCurrent();
  172.  
  173.     //----------------------------------------------------------------
  174.     // ExchangePrevious() - exchange data between current and previous
  175.     //    list nodes; return pointer to new current data
  176.     //----------------------------------------------------------------
  177.     void *ExchangePrevious();
  178.  
  179.     //----------------------------------------------------------------
  180.     // ExchangeNext() - exchange data between current and next list
  181.     //    nodes; return pointer to new current data
  182.     //----------------------------------------------------------------
  183.     void *ExchangeNext();
  184.  
  185.     //----------------------------------------------------------------
  186.     // Maximum() - return maximum data; current node is the node
  187.     //    containing the maximum
  188.     //----------------------------------------------------------------
  189.     void *Maximum();
  190.  
  191.     //----------------------------------------------------------------
  192.     // Minimum() - return minimum data; current node is the node
  193.     //    containing the minimum
  194.     //----------------------------------------------------------------
  195.     void *Minimum();
  196.  
  197.     //----------------------------------------------------------------
  198.     // Sort - create a new list containing the data from this sorted
  199.     //     in ascending order. The current node in the new list will
  200.     //    be its last node and will contain the pointer to the data
  201.     //    that is considered the "largest" by the compare function.
  202.     //    This is a brute force implementation. If you want to 
  203.     //    efficiently sort large lists then the sort algorithm needs 
  204.     //    more work.
  205.     //----------------------------------------------------------------
  206.     wlist    *Sort();
  207.  
  208.     //----------------------------------------------------------------
  209.     // Reverse - reverse order the list
  210.     //----------------------------------------------------------------
  211.     wlist     *Reverse();
  212.  
  213.     //----------------------------------------------------------------
  214.     // Copy - create a copy of this; a new wlist is created.  Each
  215.     //    node of the new list contains the same data pointer as
  216.     //    the corresponding node of this. No attempt is made to 
  217.     //    copy the contents of the data structure pointed to
  218.     //    at each node. (This is a level 1 copy.)
  219.     //----------------------------------------------------------------
  220.     wlist    *Copy();
  221.     
  222.     //----------------------------------------------------------------
  223.     // SetPrintData - passes the pointer to the data print function
  224.     //    to be stored for future reference
  225.     //----------------------------------------------------------------
  226.     void SetPrintData(void (*f)(FILE *pf, void *d));
  227.  
  228.     //----------------------------------------------------------------
  229.     // SetCompareData - pass pointer to the data compare function to
  230.     //    be stored for future reference by Sort()
  231.     //----------------------------------------------------------------
  232.     void SetCompareData(int (*f)(void *a, void *b));
  233.     //----------------------------------------------------------------
  234.     // print - used for debug print - assumes all list contents
  235.     //    are the same class.  
  236.     //----------------------------------------------------------------
  237.     void print(FILE *pf);
  238.     void print();
  239. };
  240.  
  241. #endif    /* WLIST_INCLUDE */
  242.